package org.unt.logic;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;

public class UrlLogic {

	public static void main(String[] args) throws IOException {

		String text = getTextPage("http://www.imagefap.com/photo/570280124/?pgid=&gid=3907142&page=0#0");
		// http://x.fap.to/images/full/50/150/1505007223.jpg

		int k = 0;
		boolean ok = true;
		while (text.indexOf("http://x.fap.to/images/full") > -1) {

			text = text.substring(text.indexOf("http://x.fap.to/images/full"));
			String url = text.substring(0, text.indexOf("\""));
			text = text.substring(10);
			
			if (!ok) {
				ok = true;
			} else {
				fetch(url, "C:/E!/Varios/She/imagefap.com/imagefap.com" + k + ".jpg");
				k++;
				ok = false;
			}
		}

		if (true)
			return;

		String url1 = "http://www.imagefap.com/photo/570280124/?pgid=&gid=3907142&page=0#";

		for (int i = 0; i < 1000; i++) {

			String file = "C:/E!/Varios/She/imagefap.com" + i + ".jpg";

			fetch(url1 + i, file);
		}

		if (true)
			return;
		String urls = "http://www.lauraloveskatrina.com/galleries/laura/alluring_fur_laura/naked-teen-show/\n" + //
				"http://www.lauraloveskatrina.com/galleries/laura/alluring_fur_laura/naked-teen-show/\n" + //
				"http://www.lauraloveskatrina.com/galleries/laura/alluring_fur_laura/nn/fantastic-sitting-actions/\n" + //
				"http://www.lauraloveskatrina.com/galleries/laura/alluring_fur_laura/nn/slender-legs/\n" + //
				"http://www.lauraloveskatrina.com/galleries/laura/alluring_fur_laura/nn/slender-legs/\n" + //
				"http://www.lauraloveskatrina.com/galleries/laura/alluring_fur_laura/nn/teasing-lovely-teen/\n" + //
				"http://www.lauraloveskatrina.com/galleries/laura/all_black_laura/massive-body-exposing/\n" + //
				"http://www.lauraloveskatrina.com/galleries/laura/all_black_laura/nice-undressing-scene/\n" + //
				"http://www.lauraloveskatrina.com/galleries/laura/all_black_laura/nn/cover-model-teen/\n" + //
				"http://www.lauraloveskatrina.com/galleries/laura/all_black_laura/nn/pretty-gorgeous-teen/\n" + //
				"http://www.lauraloveskatrina.com/galleries/laura/all_black_laura/nn/simple-cuttie/\n" + //
				"http://www.lauraloveskatrina.com/galleries/laura/all_black_laura/nn/simple-stripping/\n" + //
				"http://www.lauraloveskatrina.com/galleries/laura/amateur_pose_laura/horny-blonde/\n" + //
				"http://www.lauraloveskatrina.com/galleries/laura/amateur_pose_laura/nn/very-hot-legs/\n" + //
				"http://www.lauraloveskatrina.com/galleries/laura/amateur_pose_laura/stripping-porn-teen/\n" + //
				"http://www.lauraloveskatrina.com/galleries/laura/art_lover_laura/nn/charming-artist/\n" + //
				"http://www.lauraloveskatrina.com/galleries/laura/art_lover_laura/nn/cover-tits-teasing/\n" + //
				"http://www.lauraloveskatrina.com/galleries/laura/art_lover_laura/nn/flippant-artist/\n" + //
				"http://www.lauraloveskatrina.com/galleries/laura/art_lover_laura/nude-art-bodies/\n" + //
				"http://www.lauraloveskatrina.com/galleries/laura/art_lover_laura/undressing-lovely-chick/\n" + //
				"http://www.lauraloveskatrina.com/galleries/laura/banana_cream_laura/kitchen-teen-stripping/\n" + //
				"http://www.lauraloveskatrina.com/galleries/laura/banana_cream_laura/nn/single-pink-nipple/";

		String[] urlsArr = urls.split("\n");
		for (int j = 0; j < urlsArr.length; j++) {

			for (int i = 1; i < 1000; i++) {

				// String url = "http://www.lauraloveskatrina.com/galleries_mgp/laura/video01_laura_cleanmirror/tiny-teens/" + nextNum("", true, i) + ".jpg";
				String url = urlsArr[j] + nextNum("", true, i) + ".jpg";
				String s1 = url.substring(0, url.lastIndexOf("/"));
				String s2 = s1.substring(s1.lastIndexOf("/") + 1);

				String file = "C:/E!/Varios/She/" + s2 + i + ".jpg";

				fetch(url, file);
			}
		}
	}

	public static void fetch(String url, String file) {

		try {
			File outputFile = getBinaryWebFile(url, file);
			System.out.print(outputFile.exists() ? "." : " ko ");
		} catch (RuntimeException e) {
			if (e.getCause() instanceof FileNotFoundException) {
				e.printStackTrace();
				return;
			}
		}
	}

	public static String nextNum(String prefix, boolean leftZero, int current) {
		return prefix + (leftZero && current < 10 ? "0" : "") + current;
	}

	public static File getBinaryWebFile(String urlStr, String outputPath) {

		URL website;
		try {
			website = new URL(urlStr);
		} catch (MalformedURLException e) {
			throw new RuntimeException("MalformedURLException : cannot create URL from " + urlStr, e);
		}

		File outputFile = new File(outputPath);

		ReadableByteChannel in;
		try {
			in = Channels.newChannel(website.openStream());
		} catch (IOException e) {
			throw new RuntimeException("IOException : cannot create new channel from " + urlStr, e);
		}

		WritableByteChannel out;
		try {
			out = new FileOutputStream(outputFile).getChannel();
		} catch (FileNotFoundException e) {
			throw new RuntimeException("FileNotFoundException : cannot open output channel from " + outputFile, e);
		}

		ByteBuffer buffer = ByteBuffer.allocateDirect(32 * 1024);
		try {
			while (in.read(buffer) != -1 || buffer.position() > 0) {
				buffer.flip();
				out.write(buffer);
				buffer.compact();
			}
		} catch (IOException e) {
			throw new RuntimeException("IOException : cannot read/write from/to input/output channels", e);
		}

		return outputFile;
	}

	public static String getTextPage(String urlStr) throws IOException {

		final java.net.URL url = new java.net.URL(urlStr);
		final java.net.URLConnection uconn = url.openConnection();

		if (!(uconn instanceof java.net.HttpURLConnection))
			throw new java.lang.IllegalArgumentException("URL protocol must be HTTP");

		final java.net.HttpURLConnection conn = (java.net.HttpURLConnection) uconn;

		// conn.setConnectTimeout(10000);
		// conn.setReadTimeout(10000);
		// conn.setInstanceFollowRedirects(true);
		// conn.setRequestProperty("User-agent", "spider");

		conn.connect();

		InputStream content = (InputStream) conn.getContent();
		BufferedInputStream b = new BufferedInputStream(content);

		StringBuffer buf = new StringBuffer();

		byte[] bytes = new byte[1024];
		int numberChars;
		while ((numberChars = b.read(bytes)) != -1) {
			buf.append(new String(bytes, 0, numberChars));
		}

		conn.disconnect();

		return buf.toString();
	}
}
